<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include_once("../libs/setup.php");
include_once("../libs/helper.php");

session_start();

if (!check_admin_session()) {
    header("Location: signin.php");
    return;
}
$smarty->assign('wwwUrl', '../');
assign_home_header($smarty);

function load_paging_accounts(mysqli &$mysqli, Smarty &$smarty) {

    $query1 = "select count(*) from `user` where `Role`!='admin'";

    $result1 = $mysqli->query($query1);

    if ($result1) {
        $row = $result1->fetch_array();
        $row_count = $row[0];

        if (isset($_REQUEST['page']))
            $page_number = preg_replace('#[^0-9]#i', '', $_REQUEST['page']);
        else
            $page_number = 1;

        $items_per_page = 20;
        $last_page = ceil($row_count / $items_per_page);

        if ($page_number < 1) {
            $page_number = 1;
        } else if ($page_number > $last_page) {
            $page_number = $last_page;
        }

        mysqli_free_result($result1);
        $limit = 'LIMIT ' . ($page_number - 1) * $items_per_page . ',' . $items_per_page;
        $query2 = "select * from `user` where `Role`!='admin'" . $limit;
        $result2 = $mysqli->query($query2);
        if ($result2) {
            $accounts = array();
            while ($row = $result2->fetch_assoc()) {
                $accounts[] = array('Ma' => $row['Ma'],
                    'Email' => $row['Email'],
                    'HoTen' => $row['HoTen'],
                    'KichHoat' => $row['KichHoat']
                );
            }
            $smarty->assign('accounts', $accounts);
            $smarty->assign('page_count', $last_page);
            $smarty->assign('page_current', $page_number);
        }
        if ($mysqli->error) {
            system_failed($mysqli->error . " sql: {$query2}", $smarty);
            return false;
        }
    }
    return true;
}

function lock_account($userID, mysqli &$mysqli, Smarty &$smarty) {
    //find user
    $query1 = "select * from `user` where `Ma`={$userID}";
    $result1 = $mysqli->query($query1);
    if ($result1) {
        if ($result1->num_rows == 0) {
            $smarty->assign('is_display', 'block');
            $smarty->assign('message', 'Không tồn tại account này.');
            $smarty->assign('type_message', 'error');
            return true;
        }
        $sql = "update `user` set `KichHoat`=0 where `Ma`={$userID}";
        $result2 = $mysqli->query($sql);
        if ($result2) {
            $smarty->assign('is_display', 'block');
            $smarty->assign('message', 'Khóa tài khoản thành công.');
            $smarty->assign('type_message', 'success');
        }
    }
    if ($mysqli->error) {
        system_failed($mysqli->error, $smarty);
        return false;
    }
    return true;
}

function activate_account($userID, mysqli &$mysqli, Smarty &$smarty) {
    //find user
    $query1 = "select * from `user` where `Ma`={$userID}";
    $result1 = $mysqli->query($query1);
    if ($result1) {
        if ($result1->num_rows == 0) {
            $smarty->assign('is_display', 'block');
            $smarty->assign('message', 'Không tồn tại account này.');
            $smarty->assign('type_message', 'error');
            return true;
        }
        $sql = "update `user` set `KichHoat`=1 where `Ma`={$userID}";
        $result2 = $mysqli->query($sql);
        if ($result2) {
            $smarty->assign('is_display', 'block');
            $smarty->assign('message', 'Khóa tài khoản thành công.');
            $smarty->assign('type_message', 'success');
        }
    }
    if ($mysqli->error) {
        system_failed($mysqli->error, $smarty);
        return false;
    }
    return true;
}

$mysqli = connect_database($smarty);
if(isset($_REQUEST['id'])&&isset($_REQUEST['action']))
{
    $action = $_REQUEST['action'];
    if($action=="lock")
    {
        
        if(!lock_account($_REQUEST['id'], $mysqli, $smarty))
        {
            return;
        }
    }
    if($action=="activate")
    {
        if(!activate_account($_REQUEST['id'], $mysqli, $smarty))
        {
            return;
        }
    }
}
if (!load_paging_accounts($mysqli, $smarty)) {
    return;
}
$smarty->display('admin/account.tpl');
